package cn.ratel.route;

import com.google.common.base.Preconditions;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.LinkedHashMap;
import java.util.Map;

public class Action {
	private Map<String, ActionParam> paramMap = new LinkedHashMap<>();
	private String name;
	private Method method;

	Action(String name, Method method) {
		this.name = name;
		this.method = method;
		initParams();
	}

	private void initParams() {
		Parameter[] parameters = method.getParameters();
		for (Parameter parameter : parameters) {
			Preconditions.checkState(parameter.isNamePresent(), "-parameters is required as javac compile options");
			String paramName = parameter.getName();
			paramMap.put(paramName, new ActionParam(parameter));
		}
	}

	public Object[] getParams() {
		if (paramMap.size() == 0) {
			return null;
		}
		Object[] params = new Object[paramMap.size()];
		int i = 0;

		for (String paramName : paramMap.keySet()) {
			params[i++] = paramMap.get(paramName).getParamValue();
		}
		return params;
	}

	public String getName() {
		return name;
	}

	public Method getMethod() {
		return method;
	}

}
